using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace AlumnoEjemplos.TP3D
{
    class Box
    {
        private Mesh _mesh;
        private Texture _texture;
        private Vector3 _position;

        public Box(Mesh newBox, Texture texture, Vector3 position)
        {
            this.Mesh = newBox;
            this.Texture = texture;
            this.Position = position;
        }

        public Vector3 Position
        {
            get {
                return _position;
            }
            set {
                _position = value;
            }
        }

        protected Texture Texture
        {
            get {
                return _texture;
            }
            set {
                _texture = value;
            }
        }

        protected Mesh Mesh
        {
            get {
                return _mesh;
            }
            set {
                _mesh = value;
            }
        }

        internal void Render()
        {
            Device d3dDevice = TgcViewer.GuiController.Instance.D3dDevice;
            d3dDevice.SetTexture(0, this.Texture);
            d3dDevice.Transform.World = Matrix.Identity * Matrix.Translation(this.Position);
            this.Mesh.DrawSubset(0);
        }

        internal static Box CreateBox(Device d3dDevice, Texture texture, Vector3 size, Vector3 position)
        {
            Mesh newBox = Mesh.Box(d3dDevice, size.X, size.Y, size.Z);
            newBox = newBox.Clone(newBox.Options.Value,
                            VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0 |
                            VertexFormats.Texture1, newBox.Device);
            using (VertexBuffer vb = newBox.VertexBuffer)
            {

                CustomVertex.PositionNormalTextured[] verts =
                    (CustomVertex.PositionNormalTextured[]) vb.Lock(0,
                                                                    typeof (CustomVertex.PositionNormalTextured),
                                                                    LockFlags.None,
                                                                    newBox.NumberVertices);
                try
                {
                    for (int i = 0; i < verts.Length; i += 4)
                    {
                        verts[i + 0].Tu = 0.0f;
                        verts[i + 0].Tv = 0.0f;
                        verts[i + 1].Tu = 1.0f;
                        verts[i + 1].Tv = 0.0f;
                        verts[i + 2].Tu = 1.0f;
                        verts[i + 2].Tv = 1.0f;
                        verts[i + 3].Tu = 0.0f;
                        verts[i + 3].Tv = 1.0f;
                    }
                }
                finally
                {
                    vb.Unlock();
                }
            }

            return new Box(newBox, texture, position);
        }

        public void MoveX(float ammount)
        {
            this.Position = this.Position + new Vector3(ammount, 0, 0);
        }
    }
}
